/*
 * Copyright 2010 J Patrick Davenport
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package v4view.web;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertNull;
import static junit.framework.Assert.assertTrue;

import org.junit.Test;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;

public class PageElementTests {

	private CleanRoomPageElement	page	= new CleanRoomPageElement();

	@Test
	public void checkTagSet() {
		this.page.setTag("html");
		assertEquals("html", this.page.getTag());
	}

	@Test
	public void checkAddLoadCallBack() {
		assertEquals(0, this.page.getLoadCallbackCount());
		assertFalse(this.page.hasLoadCallbacks());
		this.page.setRequest(new MockHttpServletRequest());
		this.page.setResponse(new MockHttpServletResponse());
		this.page.addLoadCallBack(new IPageElementCallback<CleanRoomPageElement>() {

			@Override
			public void execute(final CleanRoomPageElement _elementBeingLoaded) {
				System.out.println("Printing " + _elementBeingLoaded.getTag());
			}
		});

		final int loadCallbackCount = this.page.getLoadCallbackCount();
		this.page.load();
		assertEquals(1, loadCallbackCount);
		assertTrue(this.page.hasLoadCallbacks());
	}

	@Test
	public void checkControlIdSetAndGet() {
		assertEquals("CleanRoomPageElement", this.page.getElementId());
		final String id = "Control1";
		this.page.setElementId(id);
		assertEquals(id, this.page.getElementId());
	}

	@Test(expected = IllegalStateException.class)
	public void checkExceptionResponseNullWithRequest() {
		this.page.setRequest(new MockHttpServletRequest());
		this.page.load();
	}

	@Test(expected = IllegalStateException.class)
	public void checkExceptionRequestNullWithResponse() {
		this.page.setResponse(new MockHttpServletResponse());
		this.page.load();
	}

	@Test
	public void checkRequest() {
		assertNull(this.page.getRequest());
	}

	private static class CleanRoomPageElement extends PageElement {
		{
			this.setTag("html");
		}

		@Override
		public void render() {
			throw new UnsupportedOperationException("Lazy developer didn't do anything");
		}

	}
}
